package com.bi.utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.bi.common.ErrorCode;
import com.bi.exception.BusineseException;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtils {
    public static String md5Encode(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] md5 = md.digest(data.getBytes(StandardCharsets.UTF_8));

            // 将处理后的字节转成 16 进制，得到最终 32 个字符
            StringBuilder sb = new StringBuilder();
            for (byte b : md5) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static boolean containsSpecialCharacter(String input) {
        // 定义特殊字符的正则表达式模式
        String pattern = "[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]";

        // 创建Pattern对象
        Pattern regex = Pattern.compile(pattern);

        // 创建Matcher对象
        Matcher matcher = regex.matcher(input);

        // 检查是否匹配到特殊字符
        return matcher.find();
    }


    public static String[] arrjsonToStrArr(String tagsJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(tagsJson, String[].class);
        } catch (JsonProcessingException e) {
            throw new BusineseException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        }
    }
    public static int getDistanceByTags(List<String> tarArr, String comTagsJson ) {
        String[] comArr = arrjsonToStrArr(comTagsJson);
        int m = tarArr.size(), n = comArr.length;
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (tarArr.get(i).equals(comArr[j])) {
                    dp[i + 1][j + 1] = dp[i][j];
                }else {
                    dp[i + 1][j + 1] = Math.min(dp[i][j + 1], dp[i + 1][j]);
                }
            }
        }
        return dp[m][n];
    }
    public static int getDistanceByTags(String[] tarArr, String comTagsJson ) {
        String[] comArr = arrjsonToStrArr(comTagsJson);
        int m = tarArr.length, n = comArr.length;
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (tarArr[i].equals(comArr[j])) {
                    dp[i + 1][j + 1] = dp[i][j];
                }else {
                    dp[i + 1][j + 1] = Math.min(dp[i][j + 1], dp[i + 1][j]) + 1;
                }
            }
        }
        return dp[m][n];
    }
    public static int getDistanceByTags(String targTagsJson, String comTagsJson ) {
        String[] tarArr = arrjsonToStrArr(targTagsJson);
        return getDistanceByTags(tarArr, comTagsJson);
    }


    public static void checkNull(Object object, ErrorCode errorCode, String msg) {
        if (object == null) {
            throw new BusineseException(errorCode, msg);
        }
    }
    public static void checkNullParams(Object object) {
        checkNullParams(object,  "传入参数为空");
    }
    public static void checkNullParams(Object object, String msg) {
        checkNull(object, ErrorCode.PARAMS_ERROR, msg);
    }
    public static void checkParams(boolean flag) {
        if (flag) {
            throw new BusineseException(ErrorCode.PARAMS_ERROR, "传入参数为空");
        }
    }
    public static void check(boolean flag, ErrorCode errorCode, String msg) {
        if (flag) {
            throw new BusineseException(errorCode, msg);
        }
    }

    /**
     * 判断某时间是否在两个时间之间
     *
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param currentDate 当前时间
     * @author yanzhen
     **/
    public static boolean isBetween(Date startDate, Date endDate, Date currentDate) {
        long startMillis = startDate.getTime();
        long endMillis = endDate.getTime();
        long currentMillis = currentDate.getTime();

        return currentMillis > startMillis && currentMillis < endMillis;
    }
    /**
     * 校验文件大小，文件类型
     */

    public static void checkFileTypeSize(MultipartFile file) {
           }
}
